﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using prjBULHRM;
namespace prjQuanLyNhanSu
{
    public partial class frmCongViec : Form
    {
        public frmCongViec()
        {
            InitializeComponent();
        }
        clsBULTT_CongViec cls;
        public void LoadDuLieu()
        {
            cls = new clsBULTT_CongViec();
            dgrChucVu.DataSource = cls.SelectAll();
            txtMaChV.Enabled = false;
            txtTenChV.Enabled = false;           
        }
        private void frmCongViec_Load(object sender, EventArgs e)
        {
            LoadDuLieu();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (btnThem.Text == "&Thêm")
            {
                btnThem.Text="&Lưu Thêm";
                txtMaChV.Text = "";
                txtTenChV.Text = "";
                txtMaChV.Enabled = true;
                txtTenChV.Enabled = true;
                txtMaChV.Focus();
                btnXoa.Enabled = false;
                btnSua.Enabled = false;
            }
            else
            {
                if (txtMaChV.Text == "" || txtTenChV.Text == "")
                {
                    MessageBox.Show("Thông tin chưa đủ", "Thiếu dữ liệu", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    cls = new clsBULTT_CongViec();
                    btnThem.Text = "&Thêm";
                    cls.CongViec_ID = txtMaChV.Text;
                    cls.Name = txtTenChV.Text;
                    if (cls.TrungMa() == true)
                    {
                        MessageBox.Show("Trùng mã","Lỗi Trùng",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    }
                    else
                    {
                        cls.Insert();
                        LoadDuLieu();
                        MessageBox.Show("Thêm thành công.");
                        btnSua.Enabled = true;
                        btnXoa.Enabled = true;
                    }
                }
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (btnSua.Text == "&Sửa")
            {
                if (txtMaChV.Text == "")
                {
                    MessageBox.Show("Bạn chưa chọn dữ liệu để xóa!", "Lỗi Dữ Liệu", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    btnSua.Text = "&Lưu Sửa";
                    txtMaChV.Enabled = false;
                    txtTenChV.Enabled = true;
                    txtTenChV.Focus();
                    btnThem.Enabled = false;
                    btnXoa.Enabled = false;
                    dgrChucVu.Enabled = false;
                }
            }
            else
            {
                cls = new clsBULTT_CongViec();
                btnSua.Text = "&Sửa";
                cls.CongViec_ID = txtMaChV.Text;
                cls.Name = txtTenChV.Text;
                if (txtMaChV.Text == "" || txtTenChV.Text == "")
                {
                    MessageBox.Show("Không được rỗng.");
                }
                else
                {
                    cls.Update();
                    LoadDuLieu();
                    MessageBox.Show("Sửa thành công !");
                    btnXoa.Enabled = true;
                    btnThem.Enabled = true;
                    dgrChucVu.Enabled = true;
                }
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtMaChV.Text == "")
            {
                MessageBox.Show("Bạn chưa chọn dữ liệu để xóa.","Lỗi Dữ Liệu",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
            else
            {
                DialogResult hoi = MessageBox.Show("Bạn có chắc muốn xóa dữ liệu này không?", "Xóa dữ liệu", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (hoi == DialogResult.Yes)
                {
                    try
                    {
                        cls = new clsBULTT_CongViec();
                        cls.Delete();
                        LoadDuLieu();
                        txtMaChV.Text = "";
                        txtTenChV.Text = "";
                        MessageBox.Show("Xóa thành công !");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Không xóa dữ liệu này được.");
                    }
                }
            }
        }

        private void dgrChucVu_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMaChV.Text = dgrChucVu.CurrentRow.Cells[0].Value.ToString();
            txtTenChV.Text = dgrChucVu.CurrentRow.Cells[1].Value.ToString();
        }

        private void dgrChucVu_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMaChV.Enabled = false;
            txtTenChV.Enabled = false;
            btnSua.Text = "&Sửa";
            btnThem.Text = "&Thêm";
            btnSua.Enabled = true;
            btnThem.Enabled = true;
            btnXoa.Enabled = true;
        }
    }
}
